const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
//对帖子点赞
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var commonid = event.id;
  var posttime = new Date().getTime()

 var count = await db.collection("sptrival_zan").where({
    openid,
    commonid
  }).count()

  if(count.total){
    return await db.collection("sptrival_zan").where({
      openid,
      commonid
    }).remove()
  }
  else{
        return await db.collection("sptrival_zan").add({
      data:{
          commonid,
          openid,
          posttime
        }
       })
  }
   
}